package com.hp_dzzl.mixin.mcr.borninchaosv.init;

import net.mcreator.borninchaosv.init.ItemAnimationFactory;
import net.mcreator.borninchaosv.item.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.Coerce;

import software.bernie.geckolib.animatable.GeoItem;

@Mixin(ItemAnimationFactory.class)
public class animatedItemsMixin {
    @Inject(
            method = "animatedItems",
            at = @At("HEAD"),
            cancellable = true,
            remap = false
    )
    private static void injectedAnimatedItems(@Coerce Object eventObj, CallbackInfo ci) {
        TickEvent.PlayerTickEvent event = (TickEvent.PlayerTickEvent) eventObj;
        Player player = event.player;

        // 仅在 START 阶段处理
        if (event.phase != TickEvent.Phase.START) {
            return;
        }

        ItemStack mainhandItem = player.getItemInHand(net.minecraft.world.InteractionHand.MAIN_HAND);
        ItemStack offhandItem = player.getItemInHand(net.minecraft.world.InteractionHand.OFF_HAND);

        // 检查是否为空，避免后续调用崩溃
        boolean hasMainGeo = !mainhandItem.isEmpty() && mainhandItem.getItem() instanceof GeoItem;
        boolean hasOffGeo  = !offhandItem.isEmpty() && offhandItem.getItem() instanceof GeoItem;

        if (!hasMainGeo && !hasOffGeo) {
            ci.cancel(); // 无需处理，跳过原方法
            return;
        }

        // 处理主手
        processItem(mainhandItem, player);

        // 处理副手
        processItem(offhandItem, player);

        // 阻止原方法执行（因为我们已经替换了逻辑）
        ci.cancel();
    }

    private static void processItem(ItemStack itemStack, Player player) {
        if (itemStack.isEmpty()) return;

        if (!(itemStack.getItem() instanceof GeoItem)) return;

        CompoundTag tag = itemStack.getTag();
        if (tag == null || !tag.contains("geckoAnim", 8)) { // 8 = STRING
            return;
        }

        String animation = tag.getString("geckoAnim");
        if (animation.isEmpty()) return;

        // 移除标记
        tag.remove("geckoAnim");

        // 必须是客户端世界
        Level level = player.level();
        if (!level.isClientSide) return;

        // 设置 animationprocedure 字段
        if (itemStack.getItem() instanceof StaffOfMagicArrowsAItem) {
            ((StaffOfMagicArrowsAItem) itemStack.getItem()).animationprocedure = animation;
        } else if (itemStack.getItem() instanceof BonescallerStaffItem) {
            ((BonescallerStaffItem) itemStack.getItem()).animationprocedure = animation;
        } else if (itemStack.getItem() instanceof PumpkinstaffaItem) {
            ((PumpkinstaffaItem) itemStack.getItem()).animationprocedure = animation;
        } else if (itemStack.getItem() instanceof PumpkinhandgunItem) {
            ((PumpkinhandgunItem) itemStack.getItem()).animationprocedure = animation;
        }
    }
}
